package com.code2roc.jarmanage.db;

import com.code2roc.jarmanage.model.ProjectDO;
import com.code2roc.jarmanage.util.CommonUtil;
import com.code2roc.jarmanage.util.DataBaseUtil;

import java.util.ArrayList;
import java.util.List;

public class ProjectDB {

    public static void insert(ProjectDO entity) {
        entity.setProjectID(CommonUtil.getNewGuid());
        entity.setCreateDate(CommonUtil.getDateFormatToSecond());
        DataBaseUtil.insert("DepolyProject", entity);
    }

    public static void delete(String projectID) {
        DataBaseUtil.executeSQL("delete from DepolyProject where projectID='" + projectID + "'");
    }

    public static void update(ProjectDO entity) {
        DataBaseUtil.update("DepolyProject", entity, "projectID");
    }

    public static ProjectDO selectOne(String projectID) {
        return DataBaseUtil.selectOne(ProjectDO.class, "DepolyProject", "projectID", projectID);
    }

    public static boolean checkExistServiceName(String serviceName) {
        String sql = "select count(1) from DepolyProject where serviceName='" + serviceName + "'";
        return DataBaseUtil.executeSQLForQueryInt(sql) > 0;
    }


    public static boolean checkExistServiceName(String serviceName, String projectID) {
        String sql = "select count(1) from DepolyProject where serviceName='" + serviceName + "' and projectID !='" + projectID + "'";
        return DataBaseUtil.executeSQLForQueryInt(sql) > 0;
    }

    public static boolean checkExistDepolyPort(String depolyPort) {
        String sql = "select count(1) from DepolyProject where depolyPort='" + depolyPort + "' or warPort = '" + depolyPort + "'";
        return DataBaseUtil.executeSQLForQueryInt(sql) > 0;
    }

    public static boolean checkExistDepolyPort(String depolyPort, String projectID) {
        String sql = "select count(1) from DepolyProject where (" + "depolyPort='" + depolyPort + "' or " + "warPort='" + depolyPort + "') and projectID !='" + projectID + "'";
        return DataBaseUtil.executeSQLForQueryInt(sql) > 0;
    }


    public static List<ProjectDO> selectList(String where) {
        return DataBaseUtil.selectList(ProjectDO.class, "DepolyProject", where);
    }
}
